package org.cboard.security.token;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.cboard.security.utils.TreeDesUtil;
import org.springframework.beans.factory.annotation.Value;

/**
 * Created by zengxiangbin on 2017/1/17.
 */
public class TokenUtils {

    public static String userIDKey = "userId";

    public static String boardIDKey = "boardId";

    public static String themeKey = "theme";

    private static String threedesKey = "A1B2C3D4E5F60708";

    public static boolean isValidated(String token) {

        boolean result = false;

        return result;

    }

    /**
     * 生产token
     *
     * @param boardId boar id
     * @param userId  user Id
     * @return
     */
    public static String genToken(Long boardId, Long userId,String theme) throws Exception {

        JSONObject tokenObj = new JSONObject();
        tokenObj.put(userIDKey, userId);
        tokenObj.put(boardIDKey, boardId);
        tokenObj.put(themeKey, theme);
        String jsonStr = tokenObj.toJSONString();
        return TreeDesUtil.encrypt(jsonStr, threedesKey);
    }

    /**
     * 解析token
     *
     * @param token
     * @return
     */
    public static JSONObject parseToken(String token) throws Exception {

        String tokenJSON = TreeDesUtil.decrypt(token, threedesKey);
        JSONObject tokenObj = JSON.parseObject(tokenJSON);
        return tokenObj;
    }


}
